import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import refresh from '@dfeidao/atom-web/url/refresh';
import { productid} from '../../atom/config';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// 这里是保存
	const cabinet_staff_no=fd.data.node.querySelector<HTMLInputElement>('[data-id="cabinet_staff_no"]').value;
	const staff_name=fd.data.node.querySelector<HTMLInputElement>('[data-id="staff_name"]').value;
	const contact_number=fd.data.node.querySelector<HTMLInputElement>('[data-id="contact_number"]').value;
	const post=fd.data.node.querySelector<HTMLSelectElement>('[data-id="post"]');
	const post_no=post.value === "请选择" ? '' :post.value;
	const post_name=post.selectedOptions[0].text;
	const deptname=fd.data.node.querySelector<HTMLSelectElement>('[data-id="deptname"]');
	const department_no=deptname.value === "请选择" ? '' :deptname.value;
	const department=deptname.selectedOptions[0].text;
	const project=fd.data.node.querySelector<HTMLSelectElement>('[data-id="project"]');
	const project_no=project.value === "请选择" ? '' :project.value;
	const project_name=project.value === "请选择" ? '' :project.selectedOptions[0].text;
	console.log("获取项目名称",project_name);
	const resident=fd.data.node.querySelector<HTMLSelectElement>('[data-id="is_resident"]');
	const is_resident=resident.value === "请选择" ? '' :resident.value;
	if (cabinet_staff_no === "" || staff_name === "" || contact_number === "" || post_no === "" || department_no === "" || is_resident === "") {
		error("必填项不能为空");
	return;
	}
	const telReg=/^1[34578]\d{9}$/;
	const contact=/^(^0\d{2}-?\d{8}$)|(^0\d{3}-?\d{7}$)|(^0\d2-?\d{8}$)|(^0\d3-?\d{7}$)$/;
	if (!telReg.test(contact_number) && !contact.test(contact_number)) {
		error("请输入正确的联系方式");
		return;
	}
	if (!(/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/.test(staff_name))) {
		error("请输入正确的姓名");
		return;
	}
	if (!/^[a-zA-Z0-9_]+$/.test(cabinet_staff_no)) {
		error("工号不能有特殊字符和中文哦！");
		return;
	}
	const staff_nation=fd.data.node.querySelector<HTMLInputElement>('[data-id="staff_nation"]').value;
	const se=fd.data.node.querySelector<HTMLSelectElement>('[data-id="sex"]');
	const sex=se.value === "请选择" ? '' :se.value;
	const state=fd.data.node.querySelector<HTMLSelectElement>('[data-id="staff_state"]');
	const staff_state=state.value === "请选择" ? '' :state.value;
	const edu=fd.data.node.querySelector<HTMLSelectElement>('[data-id="education"]');
	const education=edu.value === "请选择" ? '' :edu.value;
	const marital=fd.data.node.querySelector<HTMLSelectElement>('[data-id="marital_status"]');
	const marital_status=marital.value === "请选择" ? '' :marital.value;
	const native_place=fd.data.node.querySelector<HTMLInputElement>('[data-id="native_place"]').value;
	const politics=fd.data.node.querySelector<HTMLSelectElement>('[data-id="politics_face"]');
	const politics_face=politics.value === "请选择" ? '' :politics.value;
	const family_address=fd.data.node.querySelector<HTMLInputElement>('[data-id="family_address"]').value;
	
	if (staff_nation !== "") {
        if (!(/^[\u0391-\uFFE5]+$/.test(staff_nation))) {
			error("请输入正确的民族");
			return;
		}
	}
    if (family_address !== "") {
        if (!(/^(?=.*?[\u4E00-\u9FA5])[\dA-Za-z\u4E00-\u9FA5]+/.test(family_address))) {
			error("请输入正确的家庭住址");
			return;
		}
	}
	if (native_place !== "") {
		if (!(/^(?=.*?[\u4E00-\u9FA5])[\dA-Za-z\u4E00-\u9FA5]+/.test(family_address))) {
			error("请输入正确的籍贯");
			return;
		}
	}
	
		if (project_no !== '') {// 当用户选择了项目编号的时候    
		const res = await nodejs<{code: 0|1;result: string}>('staff-management/zj-000002/s002',{
			filter:{
				productid,
				cabinet_staff_no,
				staff_name,
				staff_nation,
				sex,
				staff_state,
				contact_number,
				education,
				marital_status,
				native_place,
				politics_face,
				family_address,
				department_no,
				department,
				post_name,
				post_no,
				project_no,
				project_name,
				is_resident
			}
		});
		if (res.code === 1) {
			success("添加成功");
			refresh();// 刷新
		}
		if (res.code === 0) {
			error("添加失败");
		}
	} else {
		const res = await nodejs<{code: 0|1;result: string}>('staff-management/zj-000002/s003',{
			filter:{
				productid,
				cabinet_staff_no,
				staff_name,
				staff_nation,
				sex,
				staff_state,
				contact_number,
				education,
				marital_status,
				native_place,
				politics_face,
				family_address,
				department_no,
				department,
				post_name,
				post_no,
				project_no,
				project_name,
				is_resident
			}
		});
		if (res.code === 1) {
			success("添加成功");
			refresh();
		}
		if (res.code === 0) {
			error("添加失败");
		}
	}









}
